package sisi.suppliers;

import java.util.List;

import javax.persistence.EntityManager;

/**
 * SupplierController.java - Controller class for the <i>suppliers database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 05/12/2013
 * 
 */
public class SupplierController{

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Supplier[] get(String cerca) {
		List<Supplier> lista = getList(cerca);
			return lista.toArray(new Supplier[0]);
		}

	@SuppressWarnings("unchecked")
	public List<Supplier> getList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.companyname) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Supplier as c " + cWhere 
					+ " order by c.companyname ");

			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}

			List<Supplier> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Supplier> getListXidPort(Integer idPort) {

		String cWhere = "";
		if (idPort != null) {
			cWhere = " WHERE c.portid = "+idPort;
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Supplier as c " + cWhere 
					+ " order by c.companyname ");

			List<Supplier> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Supplier> getListPhysical(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.companyname) ) LIKE :cerca AND c.physical = true ";
		} else {
			cWhere = " WHERE c.physical = true ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Supplier as c " + cWhere 
					+ " order by c.companyname ");

			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}

			List<Supplier> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}
	
	public Supplier getxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Supplier supplierx = em.find(Supplier.class, id);
			return supplierx;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Supplier add(Supplier supplier) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(supplier);
			em.getTransaction().commit();
			supplier = refresh(supplier);
		} finally {
			em.close();
			return supplier;
		}
	}

	@SuppressWarnings("finally")
	public boolean update(Supplier supplier) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Supplier supplierx = em.find(Supplier.class, supplier.getSupplid());
			supplierx.setAddress(supplier.getAddress());
			supplierx.setCountry(supplier.getCountry());
			supplierx.setSupplid(supplier.getSupplid());
			supplierx.setCatgdesc(supplier.getCatgdesc());
			supplierx.setCatgid(supplier.getCatgid());
			supplierx.setCity(supplier.getCity());
			supplierx.setCompanyname(supplier.getCompanyname());
			supplierx.setContactname(supplier.getContactname());
			supplierx.setContacttitle(supplier.getContacttitle());
			supplierx.setDtup(supplier.getDtup());
			supplierx.setEmail(supplier.getEmail());
			supplierx.setFax(supplier.getFax());
			supplierx.setGsm(supplier.getGsm());
			supplierx.setHomepage(supplier.getHomepage());
			supplierx.setLogicDelete(supplier.getLogicDelete());
			supplierx.setNotes(supplier.getNotes());
			supplierx.setPaymentcode(supplier.getPaymentcode());
			supplierx.setPaymentterms(supplier.getPaymentterms());
			supplierx.setPhone(supplier.getPhone());
			supplierx.setPhysical(supplier.getPhysical());
			supplierx.setPortid(supplier.getPortid());
			supplierx.setPostalcode(supplier.getPostalcode());
			supplierx.setRegion(supplier.getRegion());
			supplierx.setReq(supplier.getReq());
			supplierx.setRestrictions(supplier.getRestrictions());
			supplierx.setSuppliervalut(supplier.getSuppliervalut());
			supplierx.setTrader(supplier.getTrader());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
		return false;
		}
	}

@SuppressWarnings("finally")
public Supplier refresh(Supplier supplier) {

		Supplier supplierx = supplier;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			supplierx = em.find(Supplier.class, supplier.getSupplid());
			em.flush();
			em.refresh(supplierx);
		} finally {
			em.close();
			return supplierx;
		}
}

@SuppressWarnings("finally")
public boolean remove(Supplier supplier) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Supplier supplierx = em.find(Supplier.class, supplier.getSupplid());
			em.remove(supplierx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
}

}
//
// fine
